package com.example.administrator.base.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.administrator.base.baseActivity.BaseActivity;
import com.example.administrator.base.bean.ReLogout;
import com.example.administrator.base.R;
import com.example.administrator.base.util.GsonTools;
import com.example.administrator.base.util.InputMethodUtil;
import com.example.administrator.base.util.SharedPreferencesUtils;
import java.io.IOException;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class BrokenCarDetail extends BaseActivity {

    @InjectView(R.id.Toolbar)
    android.support.v7.widget.Toolbar Toolbar;
    @InjectView(R.id.tv_number)
    TextView tvNumber;
    @InjectView(R.id.et_message)
    EditText etMessage;
    @InjectView(R.id.btn_upload)
    Button btnUpload;
    SweetAlertDialog sweetAlertDialog;
    String carnumber = "";
    String type = "";
    @InjectView(R.id.rb_nomal)
    RadioButton rbNomal;
    @InjectView(R.id.rb_broken)
    RadioButton rbBroken;
    @InjectView(R.id.rl)
    RelativeLayout rl;
    @InjectView(R.id.rg)
    RadioGroup rg;
    String repairStadus="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broken_car_detail);
        ButterKnife.inject(this);
        sweetAlertDialog = new SweetAlertDialog(this, 5);
        sweetAlertDialog.setCanceledOnTouchOutside(false);
        sweetAlertDialog.setTitleText("加载中");
        Intent intent = getIntent();
        carnumber = intent.getStringExtra("car");
        type = intent.getStringExtra("type");
        tvNumber.setText(carnumber);
        Toolbar.setTitle("赳猎人");
        Toolbar.setSubtitle("故障车");
        setSupportActionBar(Toolbar);
        Toolbar.setNavigationIcon(R.drawable.backbar);
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        etMessage.setFocusableInTouchMode(true);
        etMessage.requestFocus();
        InputMethodUtil.showInputMethod(etMessage, 998);
        if (type.equals("brokenback")) {
            rl.setVisibility(View.VISIBLE);
        }
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId==rbNomal.getId()){
                    repairStadus="0";
                }else {
                    repairStadus="3";
                }

            }
        });
    }

    @Override
    protected void onResponseSuccess(Response response, String action) {
                        ResponseBody body = response.body();
                        if (body != null) {
                            try {
                                String string = body.string();
                                final ReLogout mse = GsonTools.changeGsonToBean(string, ReLogout.class);
                                if (mse != null) {
                                    if (mse.getCode() == 1) {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                showToast("" + mse.getMessage());
                                                sweetAlertDialog.dismiss();
                                                finish();
                                            }
                                        });
                                    } else {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                showToast( "" + mse.getMessage());
                                                sweetAlertDialog.dismiss();
                                            }
                                        });
                                    }
                                } else {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            showToast("数据异常请重试");
                                            sweetAlertDialog.dismiss();
                                        }
                                    });
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    showToast("数据异常请重试");
                                    sweetAlertDialog.dismiss();
                                }
                            });
                        }
                    }




    @Override
    protected void onResponseFailure(Throwable e, String action) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast("网络异常请重试");
                sweetAlertDialog.dismiss();
            }
        });
    }

    @OnClick(R.id.btn_upload)
    public void onViewClicked() {
        String detail = etMessage.getText().toString();

        switch (type){
            case "brokencar":
                if (detail.equals("")){
                    showToast("请填写备注");
                    return;
                }
                sweetAlertDialog.show();
                reSetCar(carnumber, detail);
                break;
            case "brokenback":
                if (repairStadus.equals("")){
                    showToast("请选择需要置成的状态");
                    return;
                }
                if (detail.equals("")){
                    showToast("请填写备注");
                    return;
                }
                sweetAlertDialog.show();
                setback(carnumber, detail,repairStadus);
                break;
            case "fixcar":
                sweetAlertDialog.show();
                reSetFixCar(carnumber,detail);
                break;
        }
    }

    /*故障车登记*/
    public void reSetCar(String number, String detail) {


        RequestBody formBody = new FormBody.Builder()
                .add("action", "FAULTBIKEREGISTRATION")
                .add("adminId", String.valueOf(SharedPreferencesUtils.getParam(BrokenCarDetail.this, "adminId", "")))
                .add("bicycleNum", number)
                .add("remarks", detail)
                .build();

        final Request request = new Request.Builder()
                .url("http://operate.99bicycle.com/OperateBicyclePro/operationperson?")
                .post(formBody)
                .build();

                postRequest(request,"FAULTBIKEREGISTRATION");
    }

    /*车辆状态置回*/
    public void setback(String carnumber, String log,String type) {

        RequestBody formBody = new FormBody.Builder()
                .add("action", "REPAIRSTADUSRELIEVED")
                .add("adminId", String.valueOf(SharedPreferencesUtils.getParam(BrokenCarDetail.this, "adminId", "")))
                .add("bicycleNum", carnumber)
                .add("repairStatus", type)
                .add("remarks", log)
                .build();

        final Request request = new Request.Builder()
                .url("http://operate.99bicycle.com/OperateBicyclePro/repairPersonnel?")
                .post(formBody)
                .build();

                postRequest(request,"REPAIRSTADUSRELIEVED");

    }

    /*维修中车登记*/
    public void reSetFixCar(String number, String detail) {
       String lat= String.valueOf(SharedPreferencesUtils.getParam(BrokenCarDetail.this, "latitude", ""));
       String lon= String.valueOf(SharedPreferencesUtils.getParam(BrokenCarDetail.this, "longtitude", ""));
        RequestBody formBody = new FormBody.Builder()
                .add("action", "SETREPAIRSTADUS")
                .add("adminId", String.valueOf(SharedPreferencesUtils.getParam(BrokenCarDetail.this, "adminId", "")))
                .add("bicycleNum", number)
                .add("remarks", detail)
                .add("latitude",lat)
                .add("longitude",lon)
                .build();

        final Request request = new Request.Builder()
                .url("http://operate.99bicycle.com/OperateBicyclePro/repairPersonnel?")
                .post(formBody)
                .build();

        postRequest(request,"SETREPAIRSTADUS");

    }

}
